home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Technology Seed / ATS July '97.toast / Mac OS 8 / Docs-Interfaces-Libs / Contextual Menus / ContextTypeCMPlugin (Sample) / ContextTypeCMPlugin.cp < prev    next >
Encoding:
Text File  |  1997-04-09  |  5.9 KB  |  225 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ContextTypeCMPlugin.cp
  3.  
  4.     Contains:    <contents>
  5.  
  6.     Written by:    Guy Fullerton
  7.  
  8.     Copyright:    <copyright>
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>     1/16/97    GBF     Updating for Direct to SOM. Adding fragment initializer.
  13.          <3>     4/12/96    GBF     The parameters for ExamineContext had changed.
  14.          <2>     3/22/96    GBF     Added outNeedMoreTime parameter to ExamineContext
  15.          <1>     3/14/96    GBF     first checked in
  16. */
  17.  
  18.  
  19. // Class Header
  20. #include "ContextTypeCMPlugin.h"
  21.  
  22. // Mac OS Includes
  23. #include <AERegistry.h>
  24. #include <CodeFragments.h>
  25. #include <ContextualMenuPlugins.h>
  26.  
  27.  
  28.  
  29. // Function declarations
  30. extern pascal OSErr __initialize(CFragInitBlockPtr); // metrowerks's default initializer
  31. pascal OSErr ContextTypeCMPluginInitialize(CFragInitBlockPtr init); // our initializer
  32. OSStatus AddCommandToAEDescList(ConstStr255Param inCommandString,
  33.     SInt32 inCommandID, AEDescList* ioCommandList);
  34.  
  35.  
  36.  
  37. /*******************************************************************************
  38.  
  39.     ContextTypeCMPluginInitialize
  40.     
  41.         All plugin SOM object must somehow register themselves so clients
  42.         can instantiate them by name.  The best place that I've found to
  43.         do it is in the fragment initializer.  Using direct to SOM means
  44.         that we can't explicitly register our class name, so we have to
  45.         implicitly register it by instantiating an object.
  46.  
  47. *******************************************************************************/
  48.  
  49. pascal OSErr ContextTypeCMPluginInitialize(CFragInitBlockPtr init)
  50. {
  51. #pragma unused (init)
  52.  
  53.     OSErr theError = __initialize(init);
  54.     if (theError == noErr)
  55.     {
  56.         ContextTypeCMPlugin* thePlugin = new ContextTypeCMPlugin;
  57.         if (thePlugin != NULL)
  58.             delete thePlugin;
  59.         else
  60.             theError = memFullErr;
  61.     }
  62.  
  63.     return theError;
  64.     
  65. } // ContextTypeCMPluginInitialize
  66.  
  67.  
  68.  
  69. /*******************************************************************************
  70.  
  71.     ContextTypeCMPlugin::Initialize
  72.  
  73. *******************************************************************************/
  74.  
  75. OSStatus  ContextTypeCMPlugin::Initialize(
  76.     Environment*,
  77.     FSSpec* inFileSpec)
  78. {
  79. #pragma unused (inFileSpec)
  80.  
  81.     // we don't need to do anything special here
  82.  
  83.     return noErr;
  84.     
  85. } // ContextTypeCMPlugin::Initialize
  86.  
  87.  
  88.  
  89. /*******************************************************************************
  90.  
  91.     ContextTypeCMPlugin::ExamineContext
  92.  
  93. *******************************************************************************/
  94.  
  95. OSStatus  ContextTypeCMPlugin::ExamineContext(
  96.     Environment*,
  97.     AEDesc *inContextDescriptor,
  98.     SInt32 inTimeOutInTicks,
  99.     AEDescList* ioCommands,
  100.     Boolean* outNeedMoreTime)
  101. {
  102. #pragma unused(inTimeOutInTicks)
  103.     
  104.     // this is a quick sample that looks for text in the context descriptor
  105.     
  106.     // make sure the descriptor isn't null
  107.     if (inContextDescriptor != NULL)
  108.     {
  109.         // tell the raw type of the descriptor
  110.         Str15 theDescriptorType;
  111.         theDescriptorType[0] = 4;
  112.         *(DescType*)(&theDescriptorType[1]) = inContextDescriptor->descriptorType;
  113.         ::AddCommandToAEDescList(theDescriptorType, 1, ioCommands);
  114.         
  115.         // try to get text out of the context descriptor; make sure to
  116.         // coerce it, cuz the app may have passed an object specifier or
  117.         // styled text, etc.
  118.         AEDesc theTextDesc = { typeNull, NULL };
  119.         if (::AECoerceDesc(inContextDescriptor, typeChar, &theTextDesc) == noErr)
  120.         {
  121.             // add a text only command to our command list
  122.             ::AddCommandToAEDescList("\pWe got text!", 2, ioCommands);
  123.         }
  124.         ::AEDisposeDesc(&theTextDesc);
  125.     }
  126.     else
  127.     {
  128.         // we have a null descriptor
  129.         ::AddCommandToAEDescList("\pNULL Descriptor", 3, ioCommands);
  130.     }
  131.     
  132.     *outNeedMoreTime = false;
  133.     
  134.     return noErr;
  135.     
  136. } // ContextTypeCMPlugin::ExamineContext
  137.  
  138.  
  139.  
  140. /*******************************************************************************
  141.  
  142.     ContextTypeCMPlugin::HandleSelection
  143.  
  144. *******************************************************************************/
  145.  
  146. OSStatus ContextTypeCMPlugin::HandleSelection(
  147.     Environment*,
  148.     AEDesc *inContextDescriptor,
  149.     SInt32 inCommandID)
  150. {
  151. #pragma unused (inContextDescriptor, inCommandID)
  152.  
  153.     // here is where you would actually carry out the action that the user
  154.     // requested.
  155.     
  156.     return noErr;
  157.     
  158. } // ContextTypeCMPlugin::HandleSelection
  159.  
  160.  
  161.  
  162. /*******************************************************************************
  163.  
  164.     ContextTypeCMPlugin::PostMenuCleanup
  165.  
  166. *******************************************************************************/
  167.  
  168. OSStatus ContextTypeCMPlugin::PostMenuCleanup(
  169.     Environment*)
  170. {
  171.     // we didn't allocate any buffers or cache any data in ExamineContext,
  172.     // so we don't need to clean anything up here
  173.     
  174.     return noErr;
  175.     
  176. } // ContextTypeCMPlugin::PostMenuCleanup
  177.  
  178.  
  179. /*******************************************************************************
  180.  
  181.     AddCommandToAEDescList
  182.  
  183. *******************************************************************************/
  184.  
  185. OSStatus AddCommandToAEDescList(
  186.     ConstStr255Param inCommandString,
  187.     SInt32 inCommandID,
  188.     AEDescList* ioCommandList)
  189. {
  190.     OSStatus theError = noErr;
  191.     
  192.     AERecord theCommandRecord = { typeNull, NULL };
  193.     
  194.     do
  195.     {
  196.         // create an apple event record for our command
  197.         theError = ::AECreateList(NULL, 0, true, &theCommandRecord);
  198.         if (theError != noErr) break;
  199.         
  200.         // stick the command text into the aerecord
  201.         theError = ::AEPutKeyPtr(&theCommandRecord, keyAEName, typeChar,
  202.             &inCommandString[1], inCommandString[0]);
  203.         if (theError != noErr) break;
  204.             
  205.         // stick the command ID into the AERecord
  206.         theError = ::AEPutKeyPtr(&theCommandRecord, keyContextualMenuCommandID, typeLongInteger,
  207.             &inCommandID, sizeof (inCommandID));
  208.         if (theError != noErr) break;
  209.         
  210.         // stick this record into the list of commands that we are passing back to CMM
  211.         theError = ::AEPutDesc(ioCommandList, // the list we're putting our command into
  212.                         0, // stick this command onto the end of our list
  213.                         &theCommandRecord); // the command I'm putting into the list
  214.         
  215.     } while (false);
  216.     
  217.     // clean up after ourself; dispose of the AERecord
  218.     AEDisposeDesc(&theCommandRecord);
  219.  
  220.     return theError;
  221.     
  222. } // ContextTypeCMPlugin::PostMenuCleanup
  223.  
  224.  
  225.